package com.sxsihe.heartmate.util;

import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

import com.sxsihe.heartmate.SerialDataActivity;
import com.sxsihe.heartmate.view.CurveView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 志华 on 2015/6/17.
 */
public class ECGParse {

    ArrayList<Integer> mBuffer = new ArrayList<Integer>();
    int mHeart = 0;
    int mSignal = -1;
    byte[] mRawData = new byte[1024];
    Handler mHander;
    CurveView mEcg;
    SerialDataActivity serialDataActivity;
    boolean isDraw;

    public void push(byte[] data)
    {
//        Log.i("data", "进来数据有:" + data.length);
        for (int i = 0; i < data.length; i++) {
             mBuffer.add(data[i] & 0xFF);
        }
    }

    public void setView(CurveView ecg)
    {
        mEcg = ecg;
    }

    public void setIsDraw(boolean isDraw){
        this.isDraw = isDraw;
    }


    public boolean validate(List<Integer> data, int checksum)
    {
        boolean result = false;
        int temp = 0;
        for (int index = 0; index < data.size(); index++)
        {
            temp += data.get(index);
        }
        temp &= 0xFF;
        temp = ~temp & 0xFF;
        if (checksum == temp)
            result = true;

        return result;
    }

    public void parse()
    {
        int index = 0;
        while(index < mBuffer.size())
        {
            try {
                index = 0;
                Integer sync = mBuffer.get(index);
                if (sync == 0xAA) {
                    sync = mBuffer.get(++index);
                    if (sync == 0xAA) {
                        Integer pLength = mBuffer.get(++index);
                        if (pLength == 12){
                            Log.i("notice", "!!!");
                        }
                        if(pLength>=170){
                            mBuffer.remove(0);
                            continue;
                        }
                        //验证数据包
                        List<Integer> payload = mBuffer.subList(++index, index + pLength);
                        int checksum = mBuffer.get(index + pLength);
                        if (validate(payload, checksum)) {
                            parsePayload(payload);
                        } else {
//                            Log.i("data", "数据包错误");
                        }
                        if (mBuffer.size() > index + pLength + 1) {
                            mBuffer.subList(0, index + pLength + 1).clear();
                        } else {
//                            Log.i("data", "解析太快");
                            break;
                        }
                    } else {
                        mBuffer.remove(0);
                    }
                } else {
                    mBuffer.remove(0);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
//                Log.i("error", e.toString());
                break;
            }
        }
    }

    int index = 0;
    int signal = -1;
    public void parsePayload(List<Integer> data)
    {
        int length = data.size();
        int raw = 0;

        int heart = 0;
        //解析raw
        if(length == 4)
        {
            if(data.get(0) == 0x80 && data.get(1) == 0x02)
            {
                raw = data.get(2)<<8|data.get(3);
                if(raw>=32768)
                    raw = raw-65536;
                index ++ ;
                if(index%2==0||index%5==0){
                    return;
                }
                if(isDraw) {
                    if(signal==0){
                        raw = 0;
                    }
                    mEcg.pushRaw(raw);
                    serialDataActivity.writeFile((int)(raw/2.3));
                }
            }
        }
        //解析心跳、信号质量
        if(length == 18)
        {
            int number = 0;
            int index = 0;

            for (index = 0; index < length; index++)
            {
                switch (data.get(index))
                {
                    case 0x02://poor_signal
                        index++;
                        signal = data.get(index);
                        serialDataActivity.toggleDevSignal(signal);
                        break;
                    case 0x03://heart_rate
                        index++;
                        heart = data.get(index);
                        if(signal==0) heart = 0;
                        if(isDraw)
                        serialDataActivity.setHeartRate(heart);
                         break;
                    case 0x84://debug_1
                        index++;
                        number = data.get(index);
                        index=index+number;
                        break;
                    case 0x08://config_byte
                        index++;
                        break;
                    case 0x85://debug_2
                        index++;
                        number = data.get(index);
                        index=index+number-1;
                        break;
                }

            }
        }
    }

    public void setActivity(SerialDataActivity serialDataActivity) {
        this.serialDataActivity = serialDataActivity;
    }
}
